﻿using StudentManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;

namespace StudentManagement.Repositories
{
    public static class SeedData
    {
        public static IApplicationBuilder UseDataInitializer(this IApplicationBuilder builder)
        {
            using (var scope = builder.ApplicationServices.CreateScope()) {
                var dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();

                #region User seed

                if (!dbContext.Users.Any()) {
                    dbContext.Users.AddRange(
                        new User
                        {
                            UserName = "admin",
                            UserPwd = "admin"
                        },
                        new User
                        {
                            UserName = "stone",
                            UserPwd = "stone"
                        }
                    );
                    dbContext.SaveChanges();
                }

                #endregion

                #region 学生种子数据

                if (!dbContext.Students.Any()) {
                    var students = new[] {
                    new Student {
                        Name = "张三",
                        Major = "电子商务",
                        Email = "zhangsan@qq.com",
                        EnrollmentDate = DateTime.Parse ("2016-09-01")
                    },
                    new Student {
                        Name = "李四",
                        Major = "人工智能",
                        Email = "lisi@qq.com",
                        EnrollmentDate = DateTime.Parse ("2017-09-01")
                    },
                    new Student {
                        Name = "王五",
                        Major = "英语",
                        Email = "wangwu@qq.com",
                        EnrollmentDate = DateTime.Parse ("2012-09-01")
                    }
                    };

                    foreach (Student item in students) {
                        dbContext.Students.Add(item);
                    }
                    dbContext.SaveChanges();
                }

                #endregion

                #region 教师种子数据

                if (!dbContext.Teachers.Any()) {

                    dbContext.Teachers.AddRange(
                        new Teacher { Name = "孔子", Email="kongzi@qq.com", HireDate = DateTime.Parse("1995-03-11") },
                        new Teacher { Name = "墨子",Email="mozi@qqcom", HireDate = DateTime.Parse("2003-03-11") },
                        new Teacher { Name = "荀子",Email="xunzi@qq.com", HireDate = DateTime.Parse("1990-03-11") }                       
                    );                   
                  
                    dbContext.SaveChanges();
                }

                #endregion

                #region 课程种子数据

                if (!dbContext.Courses.Any()) {
                    dbContext.Courses.AddRange(
                        new Course { CourseNo = 1050, Title = "世界历史", Credits = 3 },
                        new Course { CourseNo = 1050, Title = "中国历史", Credits = 3 },
                        new Course { CourseNo = 1050, Title = "中国文学", Credits = 3 }
                    );                   
                    dbContext.SaveChanges();
                }

                #endregion 课程种子数据

                #region 部门种子数据(权限)

                if (!dbContext.Departments.Any()) {
                    dbContext.Departments.Add(new Department
                    {
                        Name = "XXX国际学校",
                        ParentId = 0
                    });

                    dbContext.SaveChanges();
                }

                #endregion

                #region 超级管理员种子数据

                if (!dbContext.Accounts.Any()) {
                    dbContext.Accounts.Add(new Account
                    {
                        AccountName = "admin",
                        Password = "admin",
                        Name = "超级管理员"
                    });

                    dbContext.SaveChanges();
                }

                #endregion

                #region Menu种子数据

                if (!dbContext.Menus.Any()) {
                    dbContext.Menus.AddRange(
                        new Menu
                        {
                            Name = "组织机构管理",
                            Code = "Department",
                            SerialNumber = 0,
                            ParentId = 0,
                            Icon = "fa fa-link"
                        },
                        new Menu
                        {
                            Name = "角色管理",
                            Code = "Role",
                            SerialNumber = 1,
                            ParentId = 0,
                            Icon = "fa fa-link"
                        },
                        new Menu
                        {
                            Name = "用户管理",
                            Code = "User",
                            SerialNumber = 2,
                            ParentId = 0,
                            Icon = "fa fa-link"
                        },
                        new Menu
                        {
                            Name = "功能管理",
                            Code = "Department",
                            SerialNumber = 3,
                            ParentId = 0,
                            Icon = "fa fa-link"
                        }
                    );

                    dbContext.SaveChanges();
                }

                #endregion
            }


            return builder;
        }
    }
}
